var $ = require("../../utils/Ajax.js");
Page({
  data: {
    UserInfor: null,
    TopUpBeans:[
      { id: "1", number: "42", money: 6 },
      { id: "2", number: "84", money: 12 },
      { id: "3", number: "210", money: 30 },
      { id: "4", number: "420", money: 60 },
      { id: "5", number: "756", money: 108 },
      { id: "6", number: "3626", money: 518 },
    ]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "充值财富豆"
    })
  },

  onShow: function(){
    var UserInfor = wx.getStorageSync('UserInfor');
    this.setData({
      UserInfor: UserInfor
    })
  },

  // 充值
  TopUps: function(e){
    var that = this;
    var Amount = e.currentTarget.dataset.amount;
    var Number = e.currentTarget.dataset.number;
    $.Ajax("api/addRecharge","POST",{
      amount: Amount,
      bean: Number
    },function(res){
      wx.requestPayment({
        timeStamp: res.d.config.timestamp,
        nonceStr: res.d.config.nonceStr,
        package: res.d.config.package,
        signType: res.d.config.signType,
        paySign: res.d.config.paySign,
        success: function(res) {
          $.Ajax("api/uindex", "GET", {}, function (res) {
            if (res.s) {
              that.setData({
                UserInfor: res.d.user
              })
              wx.setStorageSync('UserInfor', res.d.user);
            }
          })
        },
        fail: function(res){
          wx.showToast({
            title: "充值取消",
            icon: 'success',
            duration: 1500
          })
        }
      })
    })
  }
})